/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package RestaurarImagen;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Joaquín  Ortiz Díaz
 */
public class Paso2_BSR {
 public BufferedImage aplica_paso2(BufferedImage imagOriginal, BufferedImage imagBSD){
        int width = imagOriginal.getWidth();
        int height = imagOriginal.getHeight();
        BufferedImage ejemplo = new BufferedImage(width,height, BufferedImage.TYPE_3BYTE_BGR);

        //Copiamos en ejemplo la imagen original, posteriormente la vamos a modificar
        for(int i=0;i<width;i++){
            for(int j=0; j<height;j++){
                ejemplo.setRGB(i,j,imagOriginal.getRGB(i, j));
            }
        }


        //Voy a almacenar en dos listas las posiciones de las líneas azules, y la anchura de éstas
        List listaInicioLineas = new ArrayList();
        List listaAnchoLineas = new ArrayList();

        int aux=0;//Es la variable que cuenta el ancho del scratch
        for(int i=0;i<imagBSD.getWidth();i++){
            int pix = imagBSD.getRGB(i,0);

                //Obtengo los valores RGB del pixel
                int rojo = ((pix & 0x00ff0000) >> 16);
                int verde =((pix & 0x0000ff00) >> 8);
                int azul = ((pix & 0x000000ff) >> 0);

                if(rojo==255 && verde==255 && azul==255 && aux==0){
                    listaInicioLineas.add(i);
                    aux=1;
                }
                else if (rojo==255 && verde==255 && azul==255 && aux!=0)
                    aux++;
                else if (rojo==0 && verde==0 && azul==0 && aux!=0){
                    listaAnchoLineas.add(aux);
                    aux=0;
                }
                    
        }

        //Iteramos en listaInicioLineas para tener los principios de cada scratch
        Iterator it1 = listaInicioLineas.iterator();
        Iterator it2 = listaAnchoLineas.iterator();

        while(it1.hasNext()&& it2.hasNext()){

            int b = ((Integer) it1.next()).intValue();
            int w = ((Integer) it2.next()).intValue();

            //Paso2 = calculamos las distancias
            for(int j=0;j<imagOriginal.getHeight();j++){

                //Obtengo los 6 pixeles del recinto
                int pix1 = imagOriginal.getRGB(b-3,j);
                int pix2 = imagOriginal.getRGB(b-2,j);
                int pix3 = imagOriginal.getRGB(b-1,j);
                int pix4 = imagOriginal.getRGB(b+w,j);
                int pix5 = imagOriginal.getRGB(b+w+1,j);
                int pix6 = imagOriginal.getRGB(b+w+2,j);

                //Paso3 = restauramos los pixeles

                for(int i=b-1;i<=b+(int)w/2;i++){
                    System.out.println("Pixel i="+i);
                
                    //Media de los pixeles alrededor del scratch
                    
                    int blue= (new Color(pix1)).getBlue()+(new Color(pix2)).getBlue()+(new Color(pix3)).getBlue();//+(new Color(pix4)).getBlue()+(new Color(pix5)).getBlue()+(new Color(pix6)).getBlue();//+(new Color(pix7)).getBlue()+(new Color(pix8)).getBlue()+(new Color(pix9)).getBlue()+(new Color(pix10)).getBlue();
                    int red = (new Color(pix1)).getRed()+(new Color(pix2)).getRed()+(new Color(pix3)).getRed();//+(new Color(pix4)).getRed()+(new Color(pix5)).getRed()+(new Color(pix6)).getRed();//+(new Color(pix7)).getRed()+(new Color(pix8)).getRed()+(new Color(pix9)).getRed()+(new Color(pix10)).getRed();
                    int green = (new Color(pix1)).getGreen()+(new Color(pix2)).getGreen()+(new Color(pix3)).getGreen();//+(new Color(pix4)).getGreen()+(new Color(pix5)).getGreen()+(new Color(pix6)).getGreen();//+(new Color(pix7)).getGreen()+(new Color(pix8)).getGreen()+(new Color(pix9)).getGreen()+(new Color(pix10)).getGreen();

                    int azul=blue/3;
                    int verde=green/3;
                    int rojo=red/3;

                    ejemplo.setRGB(i, j, (new Color(rojo,verde,azul)).getRGB());
                }

                for(int i=b+w;i>=b+(int)w/2;i--){


                    //Media de los pixeles alrededor del scratch

                    int blue= (new Color(pix4)).getBlue()+(new Color(pix5)).getBlue()+(new Color(pix6)).getBlue();
                    int red = (new Color(pix4)).getRed()+(new Color(pix5)).getRed()+(new Color(pix6)).getRed();
                    int green = (new Color(pix4)).getGreen()+(new Color(pix5)).getGreen()+(new Color(pix6)).getGreen();

                    int azul=blue/3;
                    int verde=green/3;
                    int rojo=red/3;

                    ejemplo.setRGB(i, j, (new Color(rojo,verde,azul)).getRGB());

                    
                }

                for(int i=b-1;i<=b+w;i++){
                    int blue= (new Color(pix1)).getBlue()+(new Color(pix2)).getBlue()+(new Color(pix3)).getBlue()+(new Color(pix4)).getBlue()+(new Color(pix5)).getBlue()+(new Color(pix6)).getBlue();//+(new Color(pix7)).getBlue()+(new Color(pix8)).getBlue()+(new Color(pix9)).getBlue()+(new Color(pix10)).getBlue();
                    int red = (new Color(pix1)).getRed()+(new Color(pix2)).getRed()+(new Color(pix3)).getRed()+(new Color(pix4)).getRed()+(new Color(pix5)).getRed()+(new Color(pix6)).getRed();//+(new Color(pix7)).getRed()+(new Color(pix8)).getRed()+(new Color(pix9)).getRed()+(new Color(pix10)).getRed();
                    int green = (new Color(pix1)).getGreen()+(new Color(pix2)).getGreen()+(new Color(pix3)).getGreen()+(new Color(pix4)).getGreen()+(new Color(pix5)).getGreen()+(new Color(pix6)).getGreen();//+(new Color(pix7)).getGreen()+(new Color(pix8)).getGreen()+(new Color(pix9)).getGreen()+(new Color(pix10)).getGreen();

                    int azul=blue/6;
                    int verde=green/6;
                    int rojo=red/6;

                    
                    ejemplo.setRGB(i, j, (new Color(rojo,verde,azul)).getRGB());
                    
                }

            }
        }
        return ejemplo;
    }

    int maximo(int a1, int a2, int a3, int a4, int a5, int a6){
        int max=a1;

        if (a2>max)
            max=a2;
        if (a3>max)
            max=a3;
        if (a4>max)
            max=a4;
        if (a5>max)
            max=a5;
        if (a6>max)
            max=a6;

        return max;
    }

    int minimo(int a1, int a2, int a3, int a4, int a5, int a6){
        int max=a1;

        if (a2<max)
            max=a2;
        if (a3<max)
            max=a3;
        if (a4<max)
            max=a4;
        if (a5<max)
            max=a5;
        if (a6<max)
            max=a6;
       

        return max;
    }

    int mediana(int a1, int a2, int a3, int a4, int a5, int a6){
        int Array[] = new int[6];
        Array[0]=a1;
        Array[1]=a2;
        Array[2]=a3;
        Array[3]=a4;
        Array[4]=a5;
        Array[5]=a6;
       
        Arrays.sort(Array);

        int med=Array[2];

        return med;
    }
}
